package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "温湿度监测项目表")
@Controller
public class AppZequipmentgrouprecordController {

    @Autowired
    AppZequipmentgrouprecordService appZequipmentgrouprecordService;

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "创建'温湿度监测项目表'表中一条信息", notes = "通过post方法请求，传入表中字段的对应信息，创建一条数据。并返回给View层")
    @RequestMapping(value = "/app_zequipmentgrouprecords/new",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createAppZequipmentgrouprecord(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "value", required = false) String value,
            @RequestParam(value = "valuetype", required = false) Integer valuetype,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        AppZequipmentgrouprecord item = new AppZequipmentgrouprecord();
        Date date = new Date();
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( value != null ){
            item.setValue(value);
        }
        if( valuetype != null ){
            item.setValuetype(valuetype);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
        AppZequipmentgrouprecord iteams = appZequipmentgrouprecordService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "删除'温湿度监测项目表'表中的某条记录", notes = "根据url传入的数据id，删除整条记录。")
    @RequestMapping(value = "/app_zequipmentgrouprecords/delete",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteAppZequipmentgrouprecord(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "app_zequipmentgrouprecord_id") Integer id
    ) {
        AppZequipmentgrouprecord item = appZequipmentgrouprecordService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
        }
        int delete = appZequipmentgrouprecordService.delete(id);
        boolean result = delete!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "修改'温湿度监测项目表'表中的某条记录", notes = "根据url传入的数据id，确定修改表中的某条记录，传入表中字段要修改的信息，不传代表不修改。并返回给View层")
    @RequestMapping(value = "/app_zequipmentgrouprecords/edit",
                    method = RequestMethod.POST,
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editAppZequipmentgrouprecord(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "value", required = false) String value,
            @RequestParam(value = "valuetype", required = false) Integer valuetype,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        AppZequipmentgrouprecord item = appZequipmentgrouprecordService.get(id);
        if (null == item) {
           return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
        }

        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( value != null ){
            item.setValue(value);
        }
        if( valuetype != null ){
            item.setValuetype(valuetype);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
        int update =appZequipmentgrouprecordService.update(item);
        boolean result = update!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @ApiOperation(value = "查询'温湿度监测项目表'表中的某条记录", notes = "根据url传入的数据id，查询对应的一条数据。")
    @RequestMapping(value = "/getAppZequipmentgrouprecordById",
                    method = {RequestMethod.GET},
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getAppZequipmentgrouprecordById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {

            AppZequipmentgrouprecord item = appZequipmentgrouprecordService.get(id);
            if (null == item) {
                return new ResponseEntity<>(ResultModel.error(item), HttpStatus.OK);
            }

             return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);

    }

    @ApiOperation(value = "查询'温湿度监测项目表'表中的多条记录或者新增某条记录", notes = "get传参：根据所需的字段进行匹配查询。数据数量取决于pageNo和pageSize；数据的先后顺序取决于sortItem，sortOrder； ")
    @RequestMapping(value = "/app_zequipmentgrouprecords",
                    method = { RequestMethod.GET },
                    produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getAppZequipmentgrouprecords(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "equipmentkey", required = false) String equipmentkey,
            @RequestParam(value = "ownkey", required = false) String ownkey,
            @RequestParam(value = "value", required = false) String value,
            @RequestParam(value = "valuetype", required = false) Integer valuetype,
            @RequestParam(value = "valuesize", required = false) String valuesize
    ) {
        AppZequipmentgrouprecord item = new AppZequipmentgrouprecord();
        if( equipmentkey != null ){
            item.setEquipmentkey(equipmentkey);
        }
        if( ownkey != null ){
            item.setOwnkey(ownkey);
        }
        if( value != null ){
            item.setValue(value);
        }
        if( valuetype != null ){
            item.setValuetype(valuetype);
        }
        if( valuesize != null ){
            item.setValuesize(valuesize);
        }
       PageInfo<AppZequipmentgrouprecord> list =  appZequipmentgrouprecordService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
       return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);

    }
}
